package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

/**
 *  ZyNewsAnalyse  中原网新闻解析类
 *  xiaonie 2012-11-22
 */
public class ZyNewsAnalyse implements AnalyseNews{
	private final String[] regexs={ "http://news.zynews.com/[0-9]{4}-[0-9]{2}/[0-9]*/content_[0-9]*[.]htm",
			"http://www.zynews.com/[a-z]*/[0-9]{4}-[0-9]{2}/[0-9]*/content_[0-9]*[.]htm",
			"http://[a-z]*[.]zynews.com/[a-z]*/[a-z]*/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]{3}[.]html", 
			"http://house.zynews.com/Html/[?][0-9]{5}[.]html",
			"http://lvyou.zynews.com/[a-z]*/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]{3}[.]html",
			"http://soc.zynews.com/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*[.]htm",
			"http://[a-z]*[.]zynews.com/[a-z]*/[a-z]*/[0-9]{6}/[0-9]*[.]html",
			"http://[a-z]*.zynews.cn/[a-z]*/[a-z]*/[0-9]*/[0-9]*.html"
			};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String str_encode=urlMeta.getEncoding();
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		if (htmltxt != null && htmltxt != "") {
			news.setUrl(url);
			if(url.startsWith("http://house.zynews.com/Html/")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div#artical_real"));
				dateStr=doc.select("div#artical_sth").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://jk.zynews.com/")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.art_con"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select(".content"));
				}
				
				dateStr=doc.select("div.art_info").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://lvyou.zynews.com/")){
				title=doc.select("h3").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.newstext"));
				dateStr=doc.select("div.ctitleinfo").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://jj.zynews.com/")){
				title=doc.select("h2").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
				dateStr=doc.select("div.info").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://www.zynews.com/s/")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.wwf"));
				dateStr=doc.select("div.lymz").text();
				date=DateUtils.matchDate(dateStr);
			}else{
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.article"));
				dateStr=doc.select("div.info").text();
				date=DateUtils.matchDate(dateStr);
			}
			if(title == null || title.trim().length() == 0){
			}
			
			if(date == null || date == 0){
				date=DateUtils.matchDate(doc.select("div.artcile_cont").text());
			}
			if(date == null || date== 0){
				date=DateUtils.matchDate(url);
			}
			if (date != null) {
				if (date > System.currentTimeMillis()) {
					date=DateUtils.matchDate(doc.select(".artcile_cont h1")
							.text());
				}
			}
			if(StringUtils.isBlank(content)){
				content=doc.select(".content").html();
			}
			news.setContent(content);
			news.setTitle(title);
			news.setClickNum(null);
			news.setDate(date);
			news.setType(1);
			if(url.indexOf("content_")!=-1){
				/**
				 * 解析用于更新的地址
				 */
				String updateUrl=null;
				String tempUpdateUrlId="";
				tempUpdateUrlId=url.substring(url.indexOf("content_")+8,url.indexOf(".htm"));
				updateUrl="http://bbs.zynews.com/reply.php?v=1&ajax=1&id="+tempUpdateUrlId+"&url="+url+"";
				if(updateUrl!=null){
					 news.setUpdateUrl(updateUrl);
					 NewsMeta update=Update(news);
					 if(update!=null){
						 news.setCommentNum(update.getCommentNum());
					 }
				}
			}
			
			String author=doc.select("div.titBar div.info").text();
			if(author!=null && author.indexOf("来源:")!=-1){
				author=StringUtils.substringAfter(author, "来源:").trim();
				author=author.replace("中原网-", "");
			}
			if(StringUtils.isBlank(author)){
				author=StringUtils.substringAfter(title, "来源：").trim();
			}
			news.setAuthor(author);
		}
		return news;
	}

	public static void main(String[] args) {
		ZyNewsAnalyse a=new ZyNewsAnalyse();
		String url="http://jk.zynews.com/news/yiyuanxinwen/201209/631910905.html";
		boolean detailPage=a.isDetailPage(url);
		System.out.println(detailPage);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl=meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL=CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String commentNumstr=responseToURL.getHtml();
					if(commentNumstr!=null){
						if(commentNumstr.indexOf("<em>")!=-1){
							commentNumstr=commentNumstr.substring(commentNumstr.indexOf("<em>")+4,commentNumstr.indexOf("</em>"));
						}
						Integer commentNum=0;
						try {
							commentNum=Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}					
						meta.setCommentNum(commentNum);
						return meta;
					}
				}
			}
		}	
		return null;
	}
	
	public boolean isNeedUpdate(){
		return true;
	}
}
